// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "PawnExtensionComponentBase.generated.h"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class RPG006_API UPawnExtensionComponentBase : public UActorComponent
{
	GENERATED_BODY()

protected:
	template<class T>
	T* GetOwningPawn() const
	{
		// 这是一个静态检查功能，在编译阶段，检查T类型是否派生自APawn，如果不是则停止编译并报错
		static_assert(TPointerIsConvertibleFromTo<T, APawn>::Value, TEXT("'T' 必须是派生自APawn的类"));
		return CastChecked<T>(GetOwner());
	}
	
	APawn* GetOwningPawn() const
	{
		return GetOwningPawn<APawn>();
	}

	template<class T>
	T* GetOwningController() const
	{
		static_assert(TPointerIsConvertibleFromTo<T, AController>::Value, TEXT("'T' 必须是派生自AController的类"));
		return GetOwningPawn<APawn>()->GetController<T>();
	}
};
